Import events from .ICS file to MySQL table using PHP

Course- PHP Tutorial >

As we know, the .ics file format is the universal calendar format that is used to store the calendar file in a text file. Let's create a PHP function that will import data from .ics file and store in MySQL database.

Here we have included a php class file called icalendar.php called iCalendar, which will read the ICS file and convert the data into an array format. Once you get the data array, you can save it or you can show it in tabular format in your webpage.

import from ics

PHP Code


parse("$filename");
$ical_data = $ical->get_all_data();

$timezone = "{$ical_data['VCALENDAR']['X-WR-TIMEZONE']}";
if (function_exists('date_default_timezone_set'))
date_default_timezone_set($timezone);
$strsql1 = "Insert into events (ID,StartDate,StartTime,EndDate,EndTime,Title,
Location,Description) values ";
if (!empty($ical_data['VEVENT'])) {
foreach ($ical_data['VEVENT'] as $key => $data) {

//get StartDate And StartTime
$start_dttimearr = explode('T', $data['DTSTART']);
$StartDate = $start_dttimearr[0];
$startTime = $start_dttimearr[1];

//get EndDate And EndTime
$end_dttimearr = explode('T', $data['DTEND']);
$EndDate = $end_dttimearr[0];
$EndTime = $end_dttimearr[1];

$strsql1.="('".$data['UID']."','".$StartDate."','".$startTime."',
'".$EndDate."','".$EndTime."','".mysql_real_escape_string($data['SUMMARY'])."',
'".mysql_real_escape_string($data['LOCATION'])."',
'".mysql_real_escape_string($data['DESCRIPTION'])."')";
$strsql1.=",";
}
$strsql1 = rtrim($strsql1, ',');

mysql_query($strsql1);
}

header('Location:index.php');
}

?>


connection.php file


host="localhost";// Your Host name
$uname="******"; // Your Database  User name
$pass="*******"; // Your Database password 
$database = "********"; // Your Database name

$connection=mysql_connect($host,$uname,$pass) 
or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected"); 
$result=mysql_select_db($database)
or die("database cannot be selected 
");
?>